package com.weetop.aklyscan.activity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.GlideImageLoader;
import com.weetop.aklyscan.base.BaseUrl;
import com.weetop.aklyscan.utils.GlideUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import ZPL.ZPLPrinterHelper;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class ImgDialog extends Dialog {
    private Context mContext;
    private ImageView img;
    private TextView button, back;
    private String pic;

    public ImgDialog(Context context, String pic) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_img);
        mContext = context;
        this.pic = pic;
        initVar();
        initView();
        initAttr();
        initAction();
    }

    public void initVar() {
    }

    public void initView() {
        img = findViewById(R.id.img);
        button = findViewById(R.id.button);
        back = findViewById(R.id.back);
    }

    public void initAttr() {
        GlideUtil.load(mContext, img, pic);
    }

    public void initAction() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (ZPLPrinterHelper.IsOpened()) {
                                Bitmap bitmap = GlideImageLoader.getBitmap(BaseUrl.url1 + pic);
                                if (TextUtils.isEmpty(MainActivity.userInfoBean.getPrint_deviation_x())) {
                                    ToastUtil.getInstance()._short(mContext, "水平偏移不能为空");
                                    return;
                                }
                                if (TextUtils.isEmpty(MainActivity.userInfoBean.getPrint_deviation_y())) {
                                    ToastUtil.getInstance()._short(mContext, "垂直偏移不能为空");
                                    return;
                                }
                                MainActivity.mZPL.start();
                                MainActivity.mZPL.printData("^LL"+bitmap.getHeight()+"\r\n");
                                MainActivity.mZPL.PW(MainActivity.userInfoBean.getPrint_paper_width());
                                MainActivity.mZPL.printBitmap(MainActivity.userInfoBean.getPrint_deviation_x(), MainActivity.userInfoBean.getPrint_deviation_x(), bitmap);
                                int i = MainActivity.mZPL.end();
//                        if(i>0){
//                            ToastUtil.getInstance()._short(mContext, "打印成功");
//                        }else{
//                            ToastUtil.getInstance()._short(mContext, "打印失败");
//                        }
                            } else {
                                ToastUtil.getInstance()._short(mContext, "打印机未连接成功");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public static Bitmap setImage(String imageStr) {
        try {
            byte[] decode = Base64.decode(imageStr, Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(decode, 0, decode.length);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
